﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Comp476Project
{
    class Hud
    {
        public List<Button> buttons;
        public Texture2D button;
        public int winWidth, winHeight;
        public SpriteFont hudFont, bigFont;
        public float indent, div, fps;
        public int frames;
        public double timer, prevTimer;
        public Player player;
        public bool endGame, victory;

        public Hud(ref Texture2D button, int winWidth, int winHeight, SpriteFont hudFont, SpriteFont bigFont, ref Player player)
        {
            this.button = button;
            this.winWidth = winWidth;
            this.winHeight = winHeight;
            this.buttons = new List<Button>();
            this.hudFont = hudFont;
            this.bigFont = bigFont;
            div = winHeight / 20;
            indent = 50;
            fps = 0;
            timer = 0;
            prevTimer = 0;
            frames = 0;
            this.player = player;
            //this.buttons.Add(new Button(ref this.button, "off", new Vector2(indent, 5 * div), hudFont));
        }

        public int Update(bool endGame, bool victory)
        {
            int n = 0;
            foreach (Button b in buttons)
            {
                b.Update();
            }
            for (int i = 0; i != buttons.Count(); i++)
            {
                if (buttons[i].clicked)
                {
                    
                    buttons[i].Reset();
                }
            }
            this.endGame = endGame;
            this.victory = victory;
            return n;
        }

        public void Draw(SpriteBatch spriteBatch, ref Camera cam, GameTime gameTime)
        {
            //String selection = "";
            //switch (MainGame.STATE)
            //{
            //    case MainGame.GAME_STATE.TITLE:
            //        selection = "";
            //        break;
            //}
            spriteBatch.Begin();
            // Menu
            Color color = Color.White;
            //DrawString(spriteBatch, hudFont, selection, new Vector2(winWidth / 2 - 125, 0 * div), color);
            //DrawString(spriteBatch, hudFont, "Select Behaviour:", new Vector2(10, 0.8f * div), color);
            //DrawString(spriteBatch, hudFont, "Add/Remove:", new Vector2(10, 5.8f * div), color);
            //spriteBatch.DrawString(hudFont, "R2:", new Vector2(40, 2.6f * div), Color.Black);
            //DrawString(spriteBatch, hudFont, "Reposition:", new Vector2(10, 8.8f * div), color);
            foreach (Button b in buttons)
            {
                b.Draw(spriteBatch);
            }
            // Control Tips
            //DrawString(spriteBatch, hudFont, "Left Click + Mouse: Move Camera", new Vector2(5, winHeight - 2 * div), color);
            //DrawString(spriteBatch, hudFont, "Mousewheel Scroll: Zoom In-Out", new Vector2(5, winHeight - 1 * div), color);
            // FPS
            timer += gameTime.TotalGameTime.TotalSeconds - prevTimer;
            prevTimer = gameTime.TotalGameTime.TotalSeconds;
            frames++;
            if (timer > 1)
            {
                fps = frames;
                timer = 0;
                frames = 0;
            }
            if (endGame && victory)
            {
                DrawString(spriteBatch, bigFont, "VICTORY!", new Vector2(winWidth / 2 - 120, winHeight / 2 - 200), Color.Yellow);
            }
            if (endGame && !victory)
            {
                DrawString(spriteBatch, bigFont, "DEFEAT!", new Vector2(winWidth / 2 - 120, winHeight / 2 - 200), Color.Red);
            }
            DrawString(spriteBatch, hudFont, "FPS: " + (float)(int)(fps * 10) / 10, new Vector2(winWidth - 100, 0 * div), color);
            int h = (int)gameTime.TotalGameTime.TotalHours % 24;
            int m = (int)gameTime.TotalGameTime.TotalMinutes % 60;
            int s = (int)gameTime.TotalGameTime.TotalSeconds % 60;
            string h0 = "", m0 = "", s0 = "";
            if (h < 10) h0 = "0";
            if (m < 10) m0 = "0";
            if (s < 10) s0 = "0";
            DrawString(spriteBatch, hudFont, "Time: " + h0 + h + ":" + m0 + m + ":" + s0 + s, new Vector2(winWidth - 175, 1 * div), Color.LightGreen);
            // Debug Info
            //DrawString(spriteBatch, hudFont, "X: " + (float)(int)(player.position.X * 1000) / 1000 + " Y: " + (float)(int)(player.position.Y * 1000) / 1000 + " Z: "+(float)(int)(player.position.Z * 1000) / 1000, new Vector2(winWidth - 300, winHeight - 1 * div), color);
            //DrawString(spriteBatch, hudFont, "Camera Zoom: " + (float)(int)(cam.distance * 100) / 100, new Vector2(winWidth - 200, winHeight - 1 * div), color);
            //DrawString(spriteBatch, hudFont, "Characters: " + chars, new Vector2(winWidth - 200, winHeight - 1 * div), color);
            spriteBatch.End();
        }

        private void DrawString(SpriteBatch s, SpriteFont f, string str, Vector2 pos, Color color)
        {
            s.DrawString(f, str, pos, Color.Black);
            s.DrawString(f, str, pos + new Vector2(2, 2), color);
        }
    }
}
